<template>
  <div class="add-car">
    <el-dialog v-el-drag-dialog="{ drag: true, full: false, resize: false }" title="员工添加" :close-on-click-modal="false"
      :visible.sync="md.state" @close="closeMd" v-if="md.state" width="80%" append-to-body>
      <!--  基本信息  -->
      <baseFrom ref="baseRef" @change="roleSelect" />
      <otherFrom ref="otherRef" v-show="sourceData.isDriver === 0" />
      <driverFrom ref="driverRef" v-show="sourceData.isDriver === 1"></driverFrom>

      <el-row style="padding: 20px 20px 20px 40px">
        <el-button type="primary" @click="onSubmit" :disabled="fm.sb.load">{{ fm.sb.text }}</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-row>

    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import elDragDialog from "@/directive/el-drag-dialog";
import UploadImg from "@/components/UploadImg";
import TpAmap from "@/views/tpls/tp-amap";
import { addCarListData } from "@/api/services/logistics.service";
import { addStaff } from "@/api/services/personMan.service";
import baseFrom from "./baseFrom";
import otherFrom from './otherFrom'
import driverFrom from './driverFrom'

export default {
  name: "AddStaff",
  directives: { elDragDialog },
  props: ["state"],
  components: { UploadImg, TpAmap, baseFrom, otherFrom, driverFrom },
  computed: {
    ...mapGetters(["lunitNum", "uname"]),
  },
  data() {
    return {
      isShowMap: false,
      md: { state: this.state },
      sourceData: {
        isDriver: 0,
        age: '',
        birthdayTime: '',
      },
      fm: {
        fname: "addCarFm",
        sb: { load: false, text: "立即添加" },
      },
    };
  },
  methods: {
    closeMd() {
      this.$emit("getMdState", false);
    },
    async onSubmit() {
      let baseRef = this.$refs.baseRef
      let otherRef = this.$refs.otherRef
      let driverRef = this.$refs.driverRef
      let baseValidate = await baseRef.validate()
      // 校验
      let otherValidate = this.sourceData.isDriver === 0 ? await otherRef.validate() : await driverRef.validate()

      if (!baseValidate || !otherValidate) return;

      let baseData = baseRef.getSource();
      let otherData = this.sourceData.isDriver === 0 ? otherRef.getSource() : driverRef.getSource()
      let params = Object.assign({}, this.sourceData, baseData, otherData)
      let loading = this.U.qloading("提交中，请稍后...");
      addStaff(params).then(res => {
        this.U.qclose(loading);
        if (res.code !== 1) {
          return this.U.qerror(res.msg);
        }
        this.closeMd();
        this.$emit("reloadDg");
      }).catch(() => {
        this.U.qclose(loading);
      })

    },
    resetForm() {
      let baseRef = this.$refs.baseRef
      baseRef.resetFields();
      if (this.sourceData.isDriver === 0) {
        let otherRef = this.$refs.otherRef
        otherRef.resetFields();
      } else {
        let driverRef = this.$refs.driverRef
        driverRef.resetFields();
      }
    },
    roleSelect({ name }) {
      if (!name) return
      this.sourceData.isDriver = name === "驾驶员" ? 1 : 0
    },
  },
};
</script>
<style lang="less" scoped>
.typeSty {
  padding: 0 20px 0 20px;
  font-weight: 600;
}
</style>
